package com.man.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.man.entity.Date;
import com.man.entity.Friends;

public class DateDao {
	static String URL="jdbc:mysql://localhost:3306/manmanage?user=root&password=root";
	public Boolean insert(Date date)
	{
		String sql="insert into date  values(?,?,?) ";
		boolean b=false;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1, date.getUserName());
			statement.setString(2,date.getDate());
			statement.setString(3, date.getThing());
		    statement.execute();
		    b=true;	
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return b;	
	}

	public List<Date> Select(Date date) 
	{	List<Date> list= new ArrayList<Date>();
		String sql="select * from date where userName=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,date.getUserName());
			ResultSet set=	statement.executeQuery();
			while(set.next())
			{	Date date2=new Date();
//				date2.setUserName(set.getString("userName"));
				date2.setDate(set.getString("date"));
				date2.setThing(set.getString("thing"));	
				list.add(date2);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	public Date oneSelect(Date date) 
	{	
		Date date2= new Date();
		String sql="select * from date where userName=? and date=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,date.getUserName());
			statement.setString(2,date.getDate());
			ResultSet set=	statement.executeQuery();
			
			while(set.next())
			{	
				date2.setDate(set.getString("date"));
				date2.setThing(set.getString("thing"));	
			}
			
		}  catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date2;
		
	}
	public Boolean delect(Date date)
	{
		Boolean b=false;
		String sql="delete from date where userName=? and  date=? ";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,date.getUserName());
			statement.setString(2, date.getDate());
			statement.execute();
			b=true;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;	
		
	}
	public Boolean updata(Date date) {
		// TODO Auto-generated method stub
		
		Boolean b=false;
		String sql="update date set thing=? where userName=? and date=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1, date.getThing());
			statement.setString(2, date.getUserName());
			statement.setString(3, date.getDate());
			statement.execute();
			b=true;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}

}
